import 'package:flutter/material.dart';
import 'package:xingsu/util/app_util.dart';
import 'package:xingsu/util/channel_manager.dart';
import 'package:xingsu/util/image_util.dart';
import 'new_more_page.dart';
import 'package:get/get.dart';

class DetailInfoHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double _extent = 580;

  final Map map;

  DetailInfoHeaderDelegate(this.map);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    Map detail_info = map["detail_info"] ?? {};
    Map data = detail_info["data"] ?? {};
    Map taskInfo = data["task_info"];
    List tags = taskInfo["GameTags"] ?? [];
    bool showPrice = taskInfo["ShowPrice"] ?? false;
    Map taskStat = taskInfo["TaskStat"];

    String backgroudColor = taskInfo["BackgroundColor"] ?? "#A66053";

    return Container(
      padding: const EdgeInsets.only(top: 50, left: 20, right: 20),
      color: const Color(0xFFA66053),
      child: Column(
        children: [
          Row(
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: loadNetworkImage(taskInfo["GameIcon"] ?? "",
                    width: 80, height: 80),
              ),
              const SizedBox(
                width: 10,
              ),
              Expanded(
                  child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    taskInfo["TaskName"] ?? "",
                    maxLines: 1,
                    style: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                        fontWeight: FontWeight.w500),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  SizedBox(
                    height: 20,
                    width: double.infinity,
                    child: ListView(
                      scrollDirection: Axis.horizontal,
                      children: tags.map((e) => tagItemWidget(e)).toList(),
                    ),
                  ),
                ],
              )),
              const SizedBox(
                width: 10,
              ),
              InkWell(
                onTap: () {
                  ChannelManager.callNative("openAppStore");
                },
                child: Container(
                  width: 60,
                  height: 30,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(5)),
                  child: const Text(
                    "试玩",
                    style: TextStyle(
                        color: Colors.black, fontWeight: FontWeight.w500),
                  ),
                ),
              )
            ],
          ),
          const SizedBox(
            height: 20,
          ),
          Container(
            padding: const EdgeInsets.all(10),
            width: double.infinity,
            decoration: BoxDecoration(
                color: const Color(0x20000000),
                borderRadius: BorderRadius.circular(5)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  "任务信息",
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
                const SizedBox(
                  height: 10,
                ),
                const Divider(
                  height: 1,
                  color: Colors.white12,
                ),
                SizedBox(
                  child: Wrap(
                    children: [
                      _infoItemWidget("预估单价", showPrice ? "-" : "未公开"),
                      _infoItemWidget("结算方式", "激活人数结算"),
                      _infoItemWidget(
                          "当前最高收益", "${taskStat["RecentMaxTaskIncome"] ?? 0}",
                          hasLine: false),
                      _infoItemWidget(
                          "投稿达人数量", "${taskStat["UserCount"] ?? 0}"),
                      _infoItemWidget("视频数量",
                          AppUtil.convertWan(taskStat["VideoCount"] ?? 0)),
                      _infoItemWidget("总播放量",
                          AppUtil.convertWan(taskStat["TotalPlayCount"] ?? 0),
                          hasLine: false),
                    ],
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(
            height: 20,
          ),
          _otherWidget()
        ],
      ),
    );
  }

  Widget _infoItemWidget(String title, String desc, {bool hasLine = true}) {
    return SizedBox(
      width: ((Get.width - 60) / 3.0).floorToDouble(),
      height: 60,
      child: Row(
        children: [
          Expanded(
              child: Container(
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  title,
                  style: const TextStyle(color: Colors.grey, fontSize: 12),
                ),
                const SizedBox(
                  height: 5,
                ),
                Text(
                  desc,
                  style: const TextStyle(color: Colors.white, fontSize: 15),
                )
              ],
            ),
          )),
          hasLine
              ? Container(
                  width: 0.5,
                  height: 20,
                  color: const Color(0x20000000),
                )
              : const SizedBox()
        ],
      ),
    );
  }

  Widget _otherWidget() {
    Map detail_info = map["detail_info"] ?? {};
    Map data = detail_info["data"] ?? {};
    Map taskInfo = data["task_info"];

    return Container(
        padding: const EdgeInsets.all(10),
        width: double.infinity,
        decoration: BoxDecoration(
            color: const Color(0x20000000),
            borderRadius: BorderRadius.circular(5)),
        child: Column(
          children: [
            _otherItemWidget("任务时间",
                "${AppUtil.timeStringFromTime(taskInfo["StartTime"] ?? 0)} - ${AppUtil.timeStringFromTime(taskInfo["EndTime"] ?? 0)}"),
            _otherItemWidget("预计打款周期", "视频发布后20-25个工作日"),
            _otherItemWidget("全场景结算", "支持抖游视频全场景结算"),
            _otherItemWidget("不支持dou+结算", "dou+获取的流量不可参与结算"),
          ],
        ));
  }

  Widget _otherItemWidget(String title, String desc) {
    return Container(
      alignment: Alignment.centerLeft,
      height: 40,
      child: Row(
        children: [
          SizedBox(
            width: 110,
            child: Text(
              title,
              style: const TextStyle(color: Colors.grey, fontSize: 13),
            ),
          ),
          const SizedBox(
            width: 5,
          ),
          Expanded(
            child: Text(
              desc,
              style: const TextStyle(color: Colors.white, fontSize: 12),
              maxLines: 1,
            ),
          )
        ],
      ),
    );
  }

  @override
  double get maxExtent => _extent;

  @override
  double get minExtent => _extent;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
